package ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.PrePareTaskCoursewareAdapter;
import adapter.PrePareTaskWorkAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.GetSubjectBean;
import bean.PrePareOrReViewDetailBean;
import student.andlei.com.student.R;
import utils.GsonUtils;
import view.MyLayoutManager;

public class PrePareTaskActivity extends BaseNetActivity {
    private TextView tv_subject,
            tv_teacher,
            tv_date_time_week,
            tv_teachinggoal,
            tv_teachingmaterials;
    private RecyclerView rc_cours,
                      //  rc_note,
                        rc_work;
    private List<PrePareOrReViewDetailBean.coursewareData> cours_list = new ArrayList();
   // private List<ItemBean> note_list = new ArrayList();
    private List<PrePareOrReViewDetailBean.taskData> work_list = new ArrayList();
    private PrePareTaskCoursewareAdapter coursewareAdapter;
   // private PrePareTaskNoteAdapter noteAdapter;
    private PrePareTaskWorkAdapter workAdapter;
    private int room_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("预习任务");
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200) {
            PrePareOrReViewDetailBean bean = (PrePareOrReViewDetailBean) GsonUtils.getInstance(this).parseComplexJArrayByCommon(baseBean, PrePareOrReViewDetailBean.class);
            cours_list = bean.courseware;
            work_list.clear();
            if(bean.task!=null){
                work_list.addAll(bean.task);
            }

            if(bean.task2!=null){
                work_list.addAll(bean.task2);
            }

            coursewareAdapter.setNewData(cours_list);
            workAdapter.setNewData(work_list);
            initdata(bean);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_preparetask;
    }

    @Override
    protected void findViews() {
        tv_subject = getView(R.id.tv_subject);
        tv_teacher = getView(R.id.tv_teacher);
        tv_date_time_week = getView(R.id.tv_date_time_week);
        tv_teachinggoal = getView(R.id.tv_teachinggoal);
        tv_teachingmaterials = getView(R.id.tv_teachingmaterials);
        rc_cours = getView(R.id.rcly_coursave);
        //rc_note = getView(R.id.rcly_note);
        rc_work = getView(R.id.rcly_work);

    }

    @Override
    protected void formatViews() {
        //创建布局管理
        rc_cours.setLayoutManager(new MyLayoutManager(this,LinearLayoutManager.VERTICAL,true));
       // rc_note.setLayoutManager(new MyLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        rc_work.setLayoutManager(new MyLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        coursewareAdapter = new PrePareTaskCoursewareAdapter(this,R.layout.item_cours_list,cours_list);
        coursewareAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("url",cours_list.get(position).link);
                bundle.putInt("type",cours_list.get(position).courseware_type);
                jumpTo(CoursewareDetailsActivity.class,bundle);
            }
        });

       // noteAdapter = new PrePareTaskNoteAdapter(this,R.layout.item_note_list,note_list);
        workAdapter = new PrePareTaskWorkAdapter(this,R.layout.item_work_list,work_list);
        rc_cours.setAdapter(coursewareAdapter);

       // rc_note.setAdapter(noteAdapter);
        rc_work.setAdapter(workAdapter);
        workAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.bt_answer:
                        Button button = (Button) view;
                        if(work_list.size()<=0){return;}
                        if(button.getText().toString().equals("答题")){
                            Bundle bundle = new Bundle();
                            bundle.putInt("task_id",work_list.get(position).task_id);
                            bundle.putInt("paper_id",work_list.get(position).paper_id);
                            bundle.putInt("student_task_id",work_list.get(position).student_task_id);
                            bundle.putString("title",tv_subject.getText().toString()+"作业");
                            if(work_list.get(position).paper_type == 3){
                                jumpTo(DoTheImageWorkActivity.class,bundle);
                            }else {
                                jumpTo(DoTheWorkActivity.class,bundle);
                            }
                        }else {
                            Bundle bundle = new Bundle();
                            bundle.putInt("task_id",work_list.get(position).task_id);
                            bundle.putInt("answer_id",work_list.get(position).answer_id);
                            bundle.putInt("student_task_id",work_list.get(position).student_task_id);
                            jumpTo(AnswerSheetActivity.class,bundle);
                        }

                        break;
                }
            }
        });
    }

    @Override
    protected void formatData() {

        loaddata();
    }
    private void loaddata(){
        Map<String, String> map = new HashMap<>();
        map.put("room_id",""+room_id);
        post("schedule/getCourseware", map, GetSubjectBean.class, false);
    }
    private  void initdata(PrePareOrReViewDetailBean bean){
        tv_date_time_week.setText(""+bean.room_date+"  "+bean.start_time+"---"+bean.end_time+"("+bean.week+")");
        tv_subject.setText(bean.room_name);
        tv_teacher.setText(bean.real_name);
        tv_teachinggoal.setText(bean.objective);
        tv_teachingmaterials.setText(bean.nodus);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        room_id = bundle.getInt("room_id");
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        loaddata();
    }
}
